package com.shebei.api.equipment_api_test.http;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.shebei.api.equipment_api_test.http.HttpClienInterface;
import org.springframework.stereotype.Component;

import java.sql.*;
import java.util.Map;

@Component
public class HttpClientSQLConnectUpdate implements HttpClienInterface {



    @Override
    public String sendRequest(String url, String requestBody, Map<String, String> head) {




        String res=doPostJson( url,  requestBody,head);

        return res;
    }



    private   String doPostJson(String url, String json,Map<String, String> head) {
        // 创建Httpclient对象

        String driverClassName = "com.mysql.cj.jdbc.Driver";
         url = "jdbc:mysql://10.1.20.57:3306/smp?serverTimezone=Hongkong&characterEncoding=UTF-8&autoReconnect=true";

        String mysqlusername = "smp";
        String password = "lY7lBKU2hhJ6b3qhIlXHMcd13XNb3SuChJZ7eiQf";
        if (head != null){
            for (String key : head.keySet()) {
                mysqlusername=head.get("userName");
                password=head.get("password");
                url=head.get("url");
            }


        }
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        JSONObject result= new JSONObject();
        try {

            Class.forName(driverClassName);
            con = DriverManager.getConnection(url, mysqlusername, password);


            String sql = json;
            pstmt = con.prepareStatement(sql);


            int rss = pstmt.executeUpdate(sql);

            result.put("data",rss);
            result.put("success","true");

        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            //关闭资源,倒关
            try {
                if (rs != null) rs.close();
                if (pstmt != null) pstmt.close();
                if (con != null) con.close();  //必须要关
            } catch (Exception e) {
            }

        }
      return result.toJSONString();






    }



}
